/**
$Id: PyCaduceus.cpp 3096 2010-07-06 07:21:14Z Bear $

Copyright (c) 2010 Nuwa Information Co., Ltd, and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Neither the name of Nuwa Information nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

$Author: Bear $
$Date: 2010-07-06 15:21:14 +0800 (星期二, 06 七月 2010) $
$Revision: 3096 $
*/

// Generated by Py++ 0.9.5, 
// modified by our hand for 'undefined call policies',
// added required type export to python and
// removed unnecessary type or member function.
#ifdef _MSC_VER
// Warning from Boost.Python.
#pragma warning(disable : 4244)
#pragma warning(disable : 4267)
#endif

#ifdef _PYTHON

#include "boost/python.hpp"

#include "boost/python/suite/indexing/vector_indexing_suite.hpp"

#include <boost/pointer_cast.hpp>

#include "Caduceus.hpp"
#include "../wsgi.h"

namespace bp = boost::python;

#define PY_ALLOW_THREADS_WRAPPER(rtype, call) \
    rtype result;                    \
    Py_BEGIN_ALLOW_THREADS           \
    try {                            \
        result = call;               \
    } catch(...) {                   \
        PyEval_RestoreThread(_save); \
        throw;                       \
    }                                \
    Py_END_ALLOW_THREADS             \
    return result;             

#define PY_ALLOW_THREADS_WRAPPER2(call) \
    Py_BEGIN_ALLOW_THREADS           \
    try {                            \
        call;                        \
    } catch(...) {                   \
        PyEval_RestoreThread(_save); \
        throw;                       \
    }                                \
    Py_END_ALLOW_THREADS

/**
@name Wrap WsgiWebServer methods.
*/
//@{
void WsgiWebServer__loadServiceWrap(::WsgiWebServer& self, const std::string& resource, const std::string& service){
    PY_ALLOW_THREADS_WRAPPER2(self.loadService(resource, service))
}

void WsgiWebServer__loadServiceConfigWrap(::WsgiWebServer& self, const std::string& config_name){
    PY_ALLOW_THREADS_WRAPPER2(self.loadServiceConfig(config_name))
}

void WsgiWebServer__setServiceOptionWrap(::WsgiWebServer& self, const std::string& resource, const std::string& name, const std::string& value){
    PY_ALLOW_THREADS_WRAPPER2(self.setServiceOption(resource, name, value))
}

void WsgiWebServer__startWrap(::WsgiWebServer& self){
    if(wsgiInit2(self.getHandler().ptr(), "", ""))
     throw std::runtime_error("Unable to initialize WSGI.");

    PY_ALLOW_THREADS_WRAPPER2(self.start())
}

void WsgiWebServer__stopWrap(::WsgiWebServer& self){
    wsgiCleanup2();
    PY_ALLOW_THREADS_WRAPPER2(self.stop())
}

void WsgiWebServer__joinWrap(::WsgiWebServer& self){
    PY_ALLOW_THREADS_WRAPPER2(self.join())
}
//@}

WsgiWebServerPtr CreateWebServerWrap(boost::python::object& handler, 
                                     const std::string& resource = std::string("/"), 
                                     const boost::uint32_t port = 8080,  
                                     //PionSchedulerFactoryPtr scheduler = PionSchedulerFactoryPtr(),
                                     const std::string& ip = std::string(), 
                                     bool log = true){
  PY_ALLOW_THREADS_WRAPPER(WsgiWebServerPtr, CreateWebServer(handler, resource, port, /*scheduler,*/ ip, log))
}

PionSchedulerFactoryPtr CreatePionSingleServiceSchedulerFactoryWrap(const boost::uint32_t threads = 8){
  PY_ALLOW_THREADS_WRAPPER(PionSchedulerFactoryPtr, CreatePionSingleServiceSchedulerFactory(threads))
}

PionSchedulerFactoryPtr CreatePionOneToOneSchedulerFactoryWrap(const boost::uint32_t threads = 8){
  PY_ALLOW_THREADS_WRAPPER(PionSchedulerFactoryPtr, CreatePionOneToOneSchedulerFactory(threads))
}

BOOST_PYTHON_MODULE(PyCaduceus){
    { //::WsgiWebServer
        typedef bp::class_< WsgiWebServer, boost::noncopyable > WsgiWebServer_exposer_t;
        WsgiWebServer_exposer_t WsgiWebServer_exposer = WsgiWebServer_exposer_t( "WsgiWebServer", bp::no_init );
        bp::scope WsgiWebServer_scope( WsgiWebServer_exposer );
        { //::WsgiWebServer::loadService
        
            typedef void ( ::WsgiWebServer::*loadService_function_type )( ::std::string const &,::std::string const & ) ;
            
            WsgiWebServer_exposer.def( 
                "loadService"
                //, loadService_function_type( &::WsgiWebServer::loadService )
                , &WsgiWebServer__loadServiceWrap
                , ( bp::arg("resource"), bp::arg("service") ) );
        
        }
        { //::WsgiWebServer::loadServiceConfig
        
            typedef void ( ::WsgiWebServer::*loadServiceConfig_function_type )( ::std::string const & ) ;
            
            WsgiWebServer_exposer.def( 
                "loadServiceConfig"
                //, loadServiceConfig_function_type( &::WsgiWebServer::loadServiceConfig )
                , &WsgiWebServer__loadServiceConfigWrap
                , ( bp::arg("config_name") ) );
        
        }
        { //::WsgiWebServer::setServiceOption
        
            typedef void ( ::WsgiWebServer::*setServiceOption_function_type )( ::std::string const &,::std::string const &,::std::string const & ) ;
            
            WsgiWebServer_exposer.def( 
                "setServiceOption"
                //, setServiceOption_function_type( &::WsgiWebServer::setServiceOption )
                , &WsgiWebServer__setServiceOptionWrap
                , ( bp::arg("resource"), bp::arg("name"), bp::arg("value") ) );
        
        }
        { //::WsgiWebServer::start
        
            typedef void ( ::WsgiWebServer::*start_function_type )(  ) ;
            
            WsgiWebServer_exposer.def( 
                "start"
                //, start_function_type( &::WsgiWebServer::start ) );
                , &WsgiWebServer__startWrap );
        
        }
        { //::WsgiWebServer::stop
        
            typedef void ( ::WsgiWebServer::*stop_function_type )(  ) ;
            
            WsgiWebServer_exposer.def( 
                "stop"
                //, stop_function_type( &::WsgiWebServer::stop ) );
                , &WsgiWebServer__stopWrap );
        
        }
        { //::WsgiWebServer::join
        
            typedef void ( ::WsgiWebServer::*join_function_type )(  ) ;
            
            WsgiWebServer_exposer.def( 
                "join"
                //, join_function_type( &::WsgiWebServer::join ) );
                , &WsgiWebServer__joinWrap );
        
        }
        bp::register_ptr_to_python< boost::shared_ptr< WsgiWebServer > >();
    }

    /*
    { //::PionSchedulerFactory
        typedef bp::class_< ::PionSchedulerFactory, boost::noncopyable > PionSchedulerFactory_exposer_t;
        PionSchedulerFactory_exposer_t PionSchedulerFactory_exposer = PionSchedulerFactory_exposer_t( "PionSchedulerFactory", bp::no_init );
        bp::scope PionSchedulerFactory_scope( PionSchedulerFactory_exposer );
        bp::register_ptr_to_python< boost::shared_ptr< ::PionSchedulerFactory > >();
    }
    */

    { //::CreateWebServer
    
        typedef ::WsgiWebServerPtr ( *CreateWebServer_function_type )( ::bp::object &,::std::string const &,::boost::uint32_t const,/*::PionSchedulerFactoryPtr,*/::std::string const &,bool );
        
        bp::def( 
            "CreateWebServer"
            //, CreateWebServer_function_type( &::CreateWebServer )
            , &CreateWebServerWrap
            , ( bp::arg("handler"), bp::arg("resource")=::std::string( "/" ), bp::arg("port")=(long unsigned int const)(8080), /*bp::arg("scheduler")=::PionSchedulerFactoryPtr( ),*/ bp::arg("ip")=::std::string( ), bp::arg("log")=(bool)(true) ) );
    
    }

    /*
    { //::CreatePionSingleServiceSchedulerFactory
    
        typedef ::PionSchedulerFactoryPtr ( *CreatePionSingleServiceSchedulerFactory_function_type )( ::boost::uint32_t const );
        
        bp::def( 
            "CreatePionSingleServiceSchedulerFactory"
            //, CreatePionSingleServiceSchedulerFactory_function_type( &::CreatePionSingleServiceSchedulerFactory )
            , &CreatePionSingleServiceSchedulerFactoryWrap
            , ( bp::arg("threads")=(long unsigned int const)(8) ) );
    
    }

    { //::CreatePionOneToOneScheduler
    
        typedef ::PionSchedulerFactoryPtr ( *CreatePionOneToOneSchedulerFactory_function_type )( ::boost::uint32_t const );
        
        bp::def( 
            "CreatePionOneToOneSchedulerFactory"
            //, CreatePionOneToOneSchedulerFactory_function_type( &::CreatePionOneToOneSchedulerFactory )
            , &CreatePionOneToOneSchedulerFactoryWrap
            , ( bp::arg("threads")=(long unsigned int const)(8) ) );
    
    }
    */

    // Initialized WSGI.
    init_wsgisup2(bp::scope().ptr());
}

#endif
